import excel from "./xls/excel";


//根据页面 导出 xls 数据文件
function export_xls() {
//excel a-z 从 1 开始

//    获取json 数据
//    变量数据demo

}

var az = ['', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

// 数组排序
var objectArraySort = function (keyName) {
    return function (objectN, objectM) {
        var valueN = objectN[keyName]
        var valueM = objectM[keyName]
        if (valueN < valueM) return 1
        else if (valueN > valueM) return -1
        else return 0
    }
}

//自定义标签变量数据转化
var replacement = function (text, user) {
    text.replace(/\$\{title\}/, user.title);
    //部门
    text.replace(/\$\{dept\}/, user.dept);
    //机构
    text.replace(/\$\{organization\}/, user.hospName);
    //用户名称
    text.replace(/\$\{user\}/, user.userName);
    // 时间
    text.replace(/\$\{start\}/, user.startDate)
    text.replace(/\$\{end\}/, user.endDate);
    text.replace(/\$\{year\}/, user.year);
    text.replace(/\$\{month\}/, user.month);
    return text;
}


//获取自定义标签变量转换、排序后的新数组
function conversion_variable(header, user) {
    let demo = {
        type: "date|customize|person|title",//string 数据展示类型[时间|自定义|审核人|标题]
        float: "left|right|center",//string 行位置
        size: 10,//int  字体大小 px
        index: 3,//int 排序 
        dateFormat: "yyyy/MM/dd",//String 格式化的时间
        show: true,//boolean 是否显示
        // [organization：机构名称、
        // dept：当前用户所属部门、
        // user：当前用户名、
        // start:起始时间、
        // end:结束时间]
        text: "制表日期 ${start} 到 ${end}"//String 替换数据变量
    }
    //json 数据
    let talbe_title = header,
        //用户数据
        userDate = user,
        // 新的配置数组
        config = [];

    talbe_title.foreach(item, index => {
        //过滤不需要显示数据
        if (!item.show) {
            console.log("过滤：", item);
        } else {
            //修改text变量值
            item.text = replacement(item.text, userDate);
            //添加到新的数组中
            //按index 添加到数组中
            config.push(item);
        }
    })
    return talbe_title.sort(objectArraySort('index'));
}
// 通过 html 配置文件 获取excel合并的单元格 表头 计算出合并的位置[A1-A4,B2-B5]...
var setTable_header = function (header) {
    //通过html解析
    header.foreach(item, index => {

    })
    //通过json数据
}
//todo 获取后台完整数据

//模拟的配置数据
let clos = [
    [
        {
            "field": "name",
            "height": 300,
            "title": "县（市、区）",
            "colspan": 1,
            "rowspan": 2,
            "align": "center",
            "totalRowText": "总和"
        },
        {
            "title": "辖区内医疗卫生机构数量（个）",
            "field": "number",
            "colspan": 1,
            "rowspan": 2,
            "align": "center",
            "totalRow": true,
            "totalRowType": "int"
        },
        {
            "field": "i1",
            "title": "医疗废物产生量（kg）",
            "colspan": 6,
            "align": "center"
        },
        {
            "field": "i10",
            "title": "医疗废物转运量（kg）",
            "colspan": 6,
            "align": "center"
        },
        {
            "title": "下周医疗废物产生预估量（kg）",
            "colspan": 1,
            "rowspan": 2,
            "align": "center",
            "totalRow": true
        },
        {
            "field": "i20",
            "title": "选取的时间段内医疗废物积存总量（kg)",
            "colspan": 1,
            "rowspan": 2,
            "align": "center",
            "totalRow": true
        },
        {
            "field": "i21",
            "title": "截止目前医疗废物积存总量（kg)",
            "colspan": 1,
            "rowspan": 2,
            "align": "center",
            "totalRow": true
        }
    ],
    [
        {
            "field": "i2",
            "title": "感染性",
            "align": "center",
            "totalRow": true
        },
        {
            "field": "i3",
            "title": "损伤性",
            "align": "center",
            "totalRow": true
        },
        {
            "field": "i4",
            "title": "病理性",
            "align": "center",
            "totalRow": true
        },
        {
            "field": "i5",
            "title": "化学性",
            "align": "center",
            "totalRow": true
        },
        {
            "field": "i6",
            "title": "药物性",
            "align": "center",
            "totalRow": true
        },
        {
            "field": "i1",
            "title": "总量",
            "align": "center",
            "totalRow": true
        },
        {
            "field": "i11",
            "title": "感染性",
            "align": "center",
            "totalRow": true
        },
        {
            "field": "i12",
            "title": "损伤性",
            "align": "center",
            "totalRow": true
        },
        {
            "field": "i13",
            "title": "病理性",
            "align": "center",
            "totalRow": true
        },
        {
            "field": "i14",
            "title": "化学性",
            "align": "center",
            "totalRow": true
        },
        {
            "field": "i15",
            "title": "药物性",
            "align": "center",
            "totalRow": true
        },
        {
            "field": "i10",
            "title": "总量",
            "align": "center",
            "totalRow": true
        }
    ]
]

// 通过 json 配置文件 获取excel合并的单元格 表头 计算出合并的位置[A1-A4,B2-B5]...

// 通过 json 配置文件 获取excel合并的单元格 表头 计算出合并的位置[A1-A4,B2-B5]...
var getXySit = function (list) {
//    获取表格内容起始行
    let start_row = 1,
//    获取表格内容起始列
        start_cols = 1,
//    表格内容存储合并单元格位置
        table_cell_config = [],
//    最大行数
        max_data_rows = 0;
    list.forEach((arry, index) => {
        arry.forEach((json, json_index) => {
            let r_x = "", r_y = "", new_XY = "";
            if(json.rowspan == undefined && json.colspan == undefined){
                max_data_rows++
            }
            //合并列
            if (json.rowspan != undefined ) {
                //复杂单元格 列
                r_x = az[start_cols] + start_row;
                r_y = az[start_cols] + (start_row + Number(json.rowspan)-1);
                new_XY = r_x + "-" + r_y;
                //每行++
                if(r_x!=r_y){
                    table_cell_config.push(new_XY);
                    start_cols++;
                }
            }
            if (json.colspan != undefined) {
                //复杂单元格 行
                r_x = az[start_cols] + start_row;
                start_cols += Number(json.colspan-1) ;
                r_y = az[start_cols] + start_row;
                new_XY = r_x + "-" + r_y;
                if(r_x!=r_y){
                    table_cell_config.push(new_XY);
                    start_cols++;
                }
            }

        })
    })
    // console.log(table_cell_config)
    return table_cell_config;
}

//数据

//单元格样式
var rowStyle = function (header) {
    return {
        'data-field-1': {
            v: header.text,
            s: {
                alignment: {
                    //水平位置
                    horizontal: header.horizontal | header.float | 'center',
                    //垂直位置
                    vertical: header.vertical | 'center',
                    //自动换行
                    wrapText: header.wrapText | true,
                },
                font: {
                    name: "Calibri",//字体
                    sz: header.size || 10
                }
            }
        }
    };
}


//格式化时间
Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

export {
    export_xls
}